﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Threading;
using RapidWebDev.Common;

namespace RapidWebDev.SearchIndex.Servers.WebServer
{
	/// <summary>
	/// HttpModule to start a web inline index thread to process indexing requests.
	/// </summary>
	public class InlineIndexServiceHttpModule : IHttpModule
	{
		private static IIndexService indexService;
		private static volatile object syncObject = new object();

		private static void StartIndexService()
		{
			if (indexService == null)
			{
				lock (syncObject)
				{
					if (indexService == null)
					{
						indexService = SpringContext.Current.GetObject<IIndexService>();
						indexService.StartWork();
					}
				}
			}
		}

		#region IHttpModule Members

		/// <summary>
		/// Dispose something when the HttpModule is disposed.
		/// </summary>
		public void Dispose()
		{
		}

		/// <summary>
		/// Initialize index service.
		/// </summary>
		/// <param name="context"></param>
		public void Init(HttpApplication context)
		{
			context.EndRequest += (sender, args) => { StartIndexService(); };
		}

		#endregion
	}
}